<?php

namespace app\api\model;

use think\Model;


class MakeLog extends Model
{

    // 表名
    protected $name = 'exam_teacher_make_log';

    //获取已阅卷学生
    public static function makeStudentNum($exams_list_id, $block_id, $type='')
    {
        switch ($type) {
            case 'alone':
                $studentIds = self::where(['exams_list_id' => $exams_list_id, 'block_id' => $block_id])->where('type','in',[1,4])->column('student_id');
                break;
            case 'dual':
                $studentIds = self::where(['exams_list_id' => $exams_list_id, 'block_id' => $block_id])->where('type','in',[2,3])->column('student_id');
                break;
            default:
                $studentIds = self::where(['exams_list_id' => $exams_list_id, 'block_id' => $block_id])->column('student_id');
                break;
        }
        return  $studentIds;
    }

    //计算学生主管题得分
    public static function calculateStudentScore($exams_list_id,$student_id)
    {
        //学生单科主观题得分
        $score = 0;
        $blockList = db('exam_paper_block')->where(['exams_list_id'=> $exams_list_id])->select();
        foreach ($blockList as $block) {
            switch ($block['type']) {
                case 'alone':
                    $studentScore = self::where(['exams_list_id'=> $exams_list_id,'student_id'=>$student_id,'block_id'=>$block['id']])->value('bscore');
                    $score = bcadd($score,$studentScore,2);
                    break;
                case 'dual':
                    $firstScore = self::where(['exams_list_id'=> $exams_list_id,'student_id'=>$student_id,'block_id'=>$block['id'],'type'=>2])->find();
                    $finalScore = self::where(['exams_list_id'=> $exams_list_id,'student_id'=>$student_id,'block_id'=>$block['id'],'type'=>3])->find();
                    $endScore = self::where(['exams_list_id'=> $exams_list_id,'student_id'=>$student_id,'block_id'=>$block['id'],'type'=>5])->find();
                    if($endScore){
                        $score = bcadd($score,$endScore['bscore'],2);
                    }else{
                        $zScore = bcadd($firstScore,$finalScore,2);
                        $averageScore = getAverage($zScore,2);
                        $score = bcadd($score,$averageScore,2);
                    }
                    break;
            }
        }
        return $score;
    }
}
